package tasks.math;

import java.util.Random;

public class RainyArray {

    private int size;
    private int[] walls;
    private final int maxSize = 10;
    private final int maxHeight = 10;

    public RainyArray(int size) {
        if(size < 3 || size > maxSize) {
            throw new IllegalArgumentException("size must be between 3 and " + maxSize + " (bounds included)");
        }
        this.size = size;
        walls = new int[size];
    }

    public void randomiseWalls() {
        Random rand = new Random();
        for(int i = 0; i < size; i++) {
            walls[i] = rand.nextInt(maxHeight);
        }
    }

    public int getWaterVolume() {
        int v = 0;
        int v0 = 0;
        int maxH = walls[0];
        int maxHi = 0;
        int h;
        for(int i = 1; i < size; i++) {
            h = walls[i];
            if(maxH <= h) {
                v += maxH * (i - maxHi - 1) - v0;
                v0 = 0;
                maxH = h;
                maxHi = i;
            } else {
                v0 += h;
            }
        }
        return v;
    }
}
